﻿using Comm520;
using RFIDSystem.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RFIDSystem
{

    public partial class IPConfig : BaseForm
    {
        DLC520Socket dlcsocket;

        public IPConfig()
        {
            InitializeComponent();
            dlcsocket = this.GetSocket(SocketPort);
            dlcsocket.MachineFinded += new DLC520Socket.MachineBeFinded(dlcsocket_MachineFinded);
        }

        private void dlcsocket_MachineFinded(string ip)
        {
            lbMachine.Invoke(new Action<ListBox, String>((l, s) => l.Items.Add(s)), new object[] { lbMachine, ip });
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            lbMachine.Items.Clear();
            dlcsocket.SearchMachine();
        }

        private void bSetDealIP_Click(object sender, EventArgs e)
        {

        }

        private void bGetDealIP_Click(object sender, EventArgs e)
        {

        }

        private void bGetIP_Click(object sender, EventArgs e)
        {
            if (lbMachine.SelectedItem == null)
            {
                MessageBox.Show("请选择左边列表中的一台机器");
                return;
            }
            string[] ips = dlcsocket.GetIP(lbMachine.SelectedItem.ToString());
            if (ips.Length > 4)
            {
                tbIP.Text = ips[0];
                tbMask.Text = ips[1];
                tbGateWay.Text = ips[2];
                tbPort.Text = ips[3];
                tbMac.Text = ips[4];               
            }            
        }

        private void bSetIP_Click(object sender, EventArgs e)
        {
            string[] ips = new string[6];
            ips[0] = tbIP.Text;
            ips[1] = tbMask.Text;
            ips[2] = tbGateWay.Text;
            ips[3] = tbPort.Text;
            ips[4] = tbMac.Text;
            ips[5] = "00";//非自动ip  01为自动

            string mac = dlcsocket.SetIP(lbMachine.SelectedItem.ToString(), ips);
            if (mac != ips[4])
            {
                MessageBox.Show("设置IP地址失败，请检查机器!");
            }
            else
            {
                MessageBox.Show("设置IP地址成功：" + mac);
            }
        }

        private void bGetRemoteIP_Click(object sender, EventArgs e)
        {
            string[] ips = dlcsocket.GetRemoteIP(lbMachine.SelectedItem.ToString());
            if (ips.Length > 2)
            {
                tbRemoteIP.Text = ips[0];
                tbRemotePort.Text = ips[1];
                tbRemoteDNS.Text = ips[2];
               
            }
            else
            {
                MessageBox.Show("取服务器IP地址失败，请检查机器!");
            }
        }

        private void bSetRemoteIP_Click(object sender, EventArgs e)
        {
            string[] ips = new string[3];
            ips[0] = tbRemoteIP.Text;
            ips[1] = tbRemotePort.Text;
            ips[2] = tbRemoteDNS.Text;
            string ip = dlcsocket.SetRemoteIP(lbMachine.SelectedItem.ToString(), ips);
            if (ip == ips[0])
            {
                MessageBox.Show("设置服务器IP地址成功：" + ip);
            }
            else
            {
                MessageBox.Show("设置服务器IP地址失败，请检查机器!");
            }
        }
    }
}
